package collection;

import java.util.Stack;

class Queue<T> {
    private final Stack<T> stack = new Stack<>();

    public void enter(T t) {
        stack.push(t);
    }

    public T out() {
        if(stack.isEmpty())
            return null;
        Stack<T> temp = new Stack<>();
        while(!stack.isEmpty())
            temp.push(stack.pop());
        T t = temp.pop();
        while(!temp.isEmpty())
            stack.push(temp.pop());
        return t;
    }
}

/**
 * 用栈实现一个队列
 */
public class QueueAndStack {
    public static void main(String[] args) {
        Queue<Integer> queue = new Queue<>();
        queue.enter(1);
        queue.enter(2);
        queue.enter(3);

        System.out.println(queue.out());
        System.out.println(queue.out());
        System.out.println(queue.out());
    }
}
